package com.book.controller;

import com.book.entity.Account;
import com.book.service.AccountService;
import com.book.util.JsonResult;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author SuperYu  2024/7/30
 */

@CrossOrigin
@Controller
@RequestMapping("/account")
@Slf4j
public class AccountController {
    @Resource
    AccountService accountService;

    @PostMapping("/login")
    public String login(String userName, String password, HttpSession session, Model model) {
        Account temp = accountService.findAccountByLogin(userName);
        if (temp != null) {
            if (temp.getPassword().equals(password)) {

                session.setAttribute("loginName", userName);
                session.setAttribute("loginUserId", temp.getId());

                log.info("--- 登录成功 ----");

                return "redirect:/";

            } else {
                model.addAttribute("errorMsg", "对不起,密码有误！");

                return "error";
            }
        }
        model.addAttribute("errorMsg", "对不起,账号有误！");
        return "error";
    }

    @GetMapping("/showByPage")
    @ResponseBody
    public JsonResult<PageInfo<Account>> showPage(int pageNum, int pageSize) {
        PageInfo<Account> pageInfo = accountService.findAccountByPage(pageNum, pageSize);
        return JsonResult.ok(200, pageInfo);
    }

    @GetMapping("/showAll")
    @ResponseBody
    public JsonResult<List<Account>> showPage() {
        List<Account> list = accountService.findAllAccount();
        return JsonResult.ok(200, list);
    }


    @PostMapping("/add")
    @ResponseBody
    public JsonResult<String> showPage(@Valid Account account, BindingResult result) {
        log.info("要新增的账号：" + account);
        // 如果出错
        if (result.hasErrors()) {
            // 得到出错信息对象
            List<FieldError> listr = result.getFieldErrors();
            for (FieldError f : listr) {
                log.info("出错的属性是：" + f.getField() + " 提示信息是：" + f.getDefaultMessage());
            }

            return JsonResult.ok(500, "属性值有误！");
        } else {
            int i = accountService.addAccount(account);
            if (i > 0) {
                return JsonResult.ok(200, "新增成功");

            }
            return JsonResult.ok(500, "新增失败");
        }
    }


    @GetMapping("/del/{id}")
    @ResponseBody
    public String del(@PathVariable int id) {
        System.out.println("要删除的账号id是：" + id);

        return "ok";
    }

    @GetMapping("/checkName")
    @ResponseBody
    public Map<String, Boolean> checkName(String username) {
        HashMap<String, Boolean> map = new HashMap<>();

        System.out.println("要检查的用户名是：" + username);

        Account account = accountService.findAccountByLogin(username);

        if (account == null) {

            map.put("valid", true);

        } else {

            map.put("valid", false);
        }
        return map;
    }

    @GetMapping("/checkNameByEdit")
    @ResponseBody
    public Map<String, Boolean> checkNameByEdit(int id, String username) {
        HashMap<String, Boolean> map = new HashMap<>();

        log.info("要检查的用户名是：" + username + "   id是：" + id);

        Account account = accountService.findAccountByEdit(id, username);

        if (account == null) {

            map.put("valid", true);

        } else {

            map.put("valid", false);
        }
        return map;
    }

}
